package cn.doctorpda.conference.ui.personal.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;

import org.greenrobot.eventbus.EventBus;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.User;
import cn.doctorpda.conference.event.GetLoginMessage;
import cn.doctorpda.conference.event.GetMessage;
import cn.doctorpda.conference.localstore.UserInfo;
import cn.doctorpda.conference.presenter.ChangeNamePresenter;
import cn.doctorpda.conference.ui.MainActivity;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.IChangePersonalBaseView;
import okhttp3.Call;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class ChangeNameDialogFragment extends DialogFragment implements
        OnClickListener, IChangePersonalBaseView {

    private FragmentActivity mActivity;

    private EditText et_new_name;
    private ChangeNamePresenter mPresenter;
    private String newName;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = getActivity();
        View inflate = inflater.inflate(R.layout.fragment_alert_nickname, null);

        initView(inflate);
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        mPresenter = new ChangeNamePresenter(this);


        return inflate;
    }

    private void initView(View inflate) {
        et_new_name = (EditText) inflate.findViewById(R.id.et_nickname);
        inflate.findViewById(R.id.btn_nickname).setOnClickListener(this);
        inflate.findViewById(R.id.btn_back).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_nickname:
                newName = et_new_name.getText().toString().trim();
                if (newName.isEmpty()) {
                    T.showShort(getContext(), "昵称不能为空");
                    break;
                }
                User user=UserInfo.getUserInfo();
                mPresenter.getNewNameData(newName,user.getMOBILE());
//                listener.setNewName(newName);
//                PersonalFragment personalFragment = new PersonalFragment();
            case R.id.btn_back:
                this.dismiss();
                break;

        }
    }


    @Override
    public void setNewView(Msg msg) {
//        T.showShort(getContext(), msg.getMsg());
        L.i("改名","回调"+msg.getMsg()+msg.getStatus());
        if (msg.getStatus().equals("success")) {
            User user = UserInfo.getUserInfo();
            if (user != null) {
                user.setNICKNAME(newName);
                UserInfo.saveUserInfo(user);
            }
            EventBus.getDefault().post(new GetLoginMessage(newName));
            T.showShort(mActivity, "昵称修改成功");
            this.dismiss();
        }else {
            T.showShort(mActivity, msg.getMsg());
        }

    }

    @Override
    public void setNewError(Call call, Exception e) {

    }
}
